/*
 * Copyright 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.starsource.forum.client.view;

import org.starsource.forum.client.domain.MessageObject;
import org.starsource.forum.client.domain.SessionObject;
import org.starsource.forum.client.handler.SessionUpdateHandler;
import org.starsource.forum.client.web.ForumService;
import org.starsource.forum.client.web.ForumServiceAsync;
import org.starsource.forum.client.widget.RichTextToolbar;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.Widget;

/**
 * A simple example of an 'about' dialog box.
 */
public class SessionFollowDialog extends DialogBox {
	private final ForumServiceAsync forumService = GWT.create(ForumService.class);
	private SessionUpdateHandler sessionUpdateHandler;
	public void setSessionUpdateHandler(SessionUpdateHandler sessionUpdateHandler) {
		this.sessionUpdateHandler = sessionUpdateHandler;
	}

  interface Binder extends UiBinder<Widget, SessionFollowDialog> { }
  private static final Binder binder = GWT.create(Binder.class);
  interface RichTextBoxStyle extends CssResource {
	    String richTextArea();
	  }

  @UiField FlexTable richTextBox;
  @UiField Button submitButton;
  @UiField Button closeButton;
  @UiField RichTextBoxStyle richTextBoxStyle;

  final RichTextArea richTextArea = new RichTextArea();

  final RichTextToolbar richTextToolbar = new RichTextToolbar();

  private SessionObject sessionObject;

  public void setSessionObject(SessionObject sessionObject) {
	  this.sessionObject = sessionObject;
  }

  public SessionFollowDialog() {

    setWidget(binder.createAndBindUi(this));

    // Use this opportunity to set the dialog's caption.
    setText("回复此帖");

    richTextArea.addStyleName(richTextBoxStyle.richTextArea());

    richTextBox.setWidget(0, 0, richTextToolbar);
    richTextBox.setWidget(1, 0, richTextArea);
    richTextToolbar.setRichTextArea(richTextArea);

    setAnimationEnabled(true);
    setGlassEnabled(true);
  }

  @UiHandler("submitButton")
  void onSubmitClicked(ClickEvent event) {
	  submitButton.setEnabled(false);
	  forumService.followSession(richTextArea.getHTML(), sessionObject.getId(), new AsyncCallback<MessageObject>(){

		@Override
		public void onFailure(Throwable caught) {
			// TODO Auto-generated method stub

			submitButton.setEnabled(true);
		}

		@Override
		public void onSuccess(MessageObject result) {
			sessionUpdateHandler.onSessionUpdate(result.getSession());
			richTextArea.setText("");

			submitButton.setEnabled(true);

			hide();

		}

	  });
  }

  @UiHandler("closeButton")
  void onCloseClicked(ClickEvent event) {
    hide();
  }

  public void focus() {
	  richTextArea.setFocus(true);
  }
}
